﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
//using System.Data.SqlClient;
using System.Runtime.InteropServices;
//using System.Drawing;
using System.Drawing.Imaging;

namespace seminarska_SI
{
    public partial class Pocetna : Form
    {
        int broj = 0;
        public string[] pp = new string[150];
        ImageFormat img;
        Bitmap bt;
        Graphics PozadinskaSlika;
        private static Bitmap bmpScreenshot;
        private static Graphics gfxScreenshot;

        public Process[] lista = Process.GetProcesses();
        
        public Pocetna()
        {
            InitializeComponent();
            
        }

        private void button3_Click(object sender, EventArgs e)
        {

        }

        private void блокирањеАпликацииToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BlokiranjeAplikacii bp = new BlokiranjeAplikacii();
            bp.Show();
        }

        Process[] proces;

        void ZemiBazaProcesi()
        {
            ListBox.Items.Clear();
            int n = 100;
            int i = 0;
            string[] procesi = new string[n];

            SqlConnection konekcija = new SqlConnection();
            konekcija.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\seminarska\\seminarska_SI\\Baza.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";

            string sqlString = "SELECT * FROM KillProcess";
            SqlCommand komanda = new SqlCommand(sqlString, konekcija);

            try
            {
                konekcija.Open();
                SqlDataReader citac = komanda.ExecuteReader();
                while (citac.Read())
                {
                    procesi[i] = citac["KillProcess"].ToString();
                    ListBox.Items.Add(citac["KillProcess"].ToString());
                    i++;

                }
                citac.Close();
                
                
                proces = Process.GetProcesses();


                for (int i1 = 0; i1 < proces.Length; i1++)
                {
                   
                    for (int n1 = 0; n1 <= i; n1++)
                    {
                        string p = procesi[n1];
                        string p1 = proces[i1].ProcessName+".exe";
                        if (p == p1)
                        {
                            proces[i1].Kill();
                        }

                    }
                }
                
            }
            catch (Exception err)
            {
            }
            finally
            {
                konekcija.Close();
            }

            
        }

        private void Pocetna_Load(object sender, EventArgs e)
        {
            PostoeckiProcesi();
            //KoristeniAplikacii();
            
        }

        void PostoeckiProcesi()
        {
            
            /*
            Process[] lista = Process.GetProcesses();
          
            SqlConnection konekcija = new SqlConnection();
            konekcija.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\seminarska\\seminarska_SI\\Baza.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";

            SqlCommand komanda = new SqlCommand();
            komanda.Connection = konekcija;
            komanda.CommandText = "INSERT INTO PostoeckiProcesi (Procesi)" + "VALUES(@Procesi)";

            for (int i = 0; i < lista.Length; i++)
            {
                komanda.Parameters.AddWithValue("@Procesi", lista[i].ProcessName);
                //listBox1.Items.Add(proces[i].ProcessName);
            }
                
            try
            {
                konekcija.Open();
                komanda.ExecuteNonQuery();
            }

            finally
            {
                konekcija.Close();

            }
             */
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            ZemiBazaProcesi();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            AktivniAplikacii aa = new AktivniAplikacii();
            aa.Show();
        }


        private void мониторингToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Monitoring m = new Monitoring();
            m.Show();
        }
        
        private void timer2_Tick(object sender, EventArgs e)
        {
            Random rnd = new Random();

            //bt = new Bitmap(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height,PixelFormat.Format32bppArgb);
            //PozadinskaSlika = Graphics.FromImage(bt);
            //PozadinskaSlika.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,Screen.PrimaryScreen.Bounds.Y, 0,0, Screen.PrimaryScreen.Bounds.Size);
            Rectangle bounds = Screen.GetBounds(Point.Empty);
            using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
            {
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
                }
                bitmap.Save("C:\\Sliki\\" + rnd.Next().ToString() +".jpg",ImageFormat.Jpeg);
            }
        }

        private void ListBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void TimerZaAktivniAplikacii_Tick(object sender, EventArgs e)
        {
             Process[] lista1 = Process.GetProcesses();

             for (int i = 0; i < lista.Length; i++)
             {
                 for (int i1 = 0; i < lista1.Length; i1++)
                 {
                     if (lista[i].ProcessName != lista1[i1].ProcessName)
                     {
                         SqlConnection konekcija = new SqlConnection();
                         konekcija.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\seminarska\\seminarska_SI\\Baza.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";

                         SqlCommand komanda = new SqlCommand();
                         komanda.Connection = konekcija;
                         komanda.CommandText = "INSERT INTO PostoeckiProcesi (Procesi)" + "VALUES(@Procesi)";
                         komanda.Parameters.AddWithValue("@Procesi", lista1[i1].ProcessName);
                         MessageBox.Show(lista1[i].ProcessName);
                         
                         try
                         {
                             konekcija.Open();
                             komanda.ExecuteNonQuery();
                         }

                         finally
                         {
                             konekcija.Close();

                         }
                     }
                 }
                 //komanda.Parameters.AddWithValue("@Procesi", lista[i].ProcessName);
                 //listBox1.Items.Add(proces[i].ProcessName);
             }
        }



    }
}
